<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\common\controller;

use app\common\lib\SessionCheck;

class Admin extends SessionCheck
{
    /**
     * 列表
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('common@admin/index');
    }

    public function getDataList(){
        $result = model('Admin')->getPageListData('*',array('module' => request()->module()));
        $this->success('操作成功','',$result);
    }

    /**
     * 图标
     * @return mixed
     */
    public function icon(){
        return $this->fetch('common@admin/icon');
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            if(empty(input('username'))){
                $this->error('请填写账号');
            }
            if(empty(input('password'))){
                $this->error('请填写密码');
            }
            if(empty(input('repassword'))){
                $this->error('请填写确认密码');
            }
            if(input('password') != input('repassword')){
                $this->error('两次密码输入不一致');
            }
            if(db('admin')->where(array('username' => input('username')))->find()){
                $this->error('用户名已存在');
            }
            $saveData = array(
                'username' => input('username'),
                'password' => md5(input('password')),
                'module' => request()->module(),
                'role_ids' => input('ids') ? implode(',',input('ids')) : '',
                'create_time' => time()
            );

            $model = new \app\common\model\Admin();
            if($model->insert($saveData)){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            //角色列表
            $roleList = db('admin_role')->where(array('module' => request()->module()))->select();
            $this->assign('roleList', $roleList);
            return $this->fetch('common@admin/add');
        }
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit(){
        if(request()->isPost()){
            if(empty(input('username'))){
                $this->error('请填写账号');
            }
            if(!empty(input('password')) || !empty(input('repassword'))){
                if(input('password') != input('repassword')){
                    $this->error('两次密码输入不一致');
                }
            }
            $id = input('id');
            $saveData = array(
                'username' => input('username'),
                'role_ids' => input('ids') ? implode(',',input('ids')) : ''
            );
            if(input('password')){
                $saveData['password'] = md5(input('password'));
            }
            $model = new \app\common\model\Admin();
            if($model->save($saveData,['id' => $id])){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $id = input('id');
            $data = db('admin')->where(array('id' => $id))->find();
            //角色列表
            $roleList = db('admin_role')->where(array('module' => request()->module()))->select();
            $roleIds = $data['role_ids'] ? explode(',',$data['role_ids']) : array();
            foreach($roleList as $k => &$v){
                $v['is_checked'] = in_array($v['id'],$roleIds) ? 1 : 0;
            }
            $this->assign('roleList', $roleList);
            $this->assign('data',$data);
            return $this->fetch('common@admin/edit');
        }
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function edit_password(){
        if(request()->isPost()){
            if(empty(input('password'))){
                $this->error('请填写密码');
            }
            if(empty(input('repassword'))){
                $this->error('请填写确认密码');
            }
            if(input('password') != input('repassword')){
                $this->error('两次密码输入不一致');
            }
            $id = $this->getLoginAdminData()['id'];
            $model = new \app\common\model\Admin();
            if($model->save(array('password' => md5(input('password'))),['id' => $id])){
                $this->success('操作成功');
            }else{
                $this->error('数据未修改');
            }
        }else{
            return $this->fetch('common@admin/edit_password');
        }
    }

    /**
     * 修改状态
     */
    public function editStatus(){
        $id = input('id',0);
        $status = input('status',1);
        $saveData = array(
            'status' => $status
        );
        $model = new \app\common\model\Admin();
        if($model->save($saveData,['id'=>$id])){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id',0);
        if($id == 1){
            $this->error('系统默认主管理员不可删除');
        }
        $model = new \app\common\model\Admin();
        if($model->where(array('id' => $id))->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}